package job;

import java.sql.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
    public static final String driver="com.mysql.jdbc.Driver";
    public static final String url="jdbc:mysql://127.0.0.1:3306/kgb762?useUnicode=true&characterEncoding=utf-8";
    public static final String user="root";
    public static final String pass="123456";
    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private DBUtil(){}

    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,pass);
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
    }
    public static void close(ResultSet rs,Statement s,Connection con){
        close(rs);
        close(s);
        close(con);
    }
    public static void close(ResultSet rs){
        try {
            if(rs!=null &&rs.isClosed()==false)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement statement){
        try {
            if(statement!=null &&statement.isClosed()==false)
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement ps){
        try {
            if(ps!=null &&ps.isClosed()==false)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection connection){
        try {
            if(connection!=null &&connection.isClosed()==false)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
